import axios from 'axios'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'
// console.log(process.env);
const request = axios.create({
  baseURL: process.env.VUE_APP_API,
  timeOut: 10000
})


//配置请求拦截器
request.interceptors.request.use(
  (config) => {
    nProgress.start()
    console.log(store);
    //获取临时标识
    const userTempId = store.state.user.userTempId
    //把标识加到请求体
    config.headers.userTempId = userTempId
    return config
  },
  (error) => {
    nProgress.done()
    return Promise.reject(error)
  })

request.interceptors.response.use(
  (response) => {
    nProgress.done()

    if (response.data.code === 200) {
      return response.data.data
    } else {
      return Promise.reject({ message: response.data.message })
    }
  },
  (error) => {
    nProgress.done()

    return Promise.reject(error)
  }
)

export default request